var bingAPI = require('./BingAPI');
var bingProperties = require('./bingProperties');
var myDate = require('./myDate').myDate;
var ET = bingProperties.ET;
var $ = require('jquery');
var fs = require('fs');
//soap.send (option, soapStr, callback);
// var userName =  'AlibaPI';
var SOAP = bingProperties.SOAP;
var STATIC = bingProperties.STATIC;
var REPORTTYPE = bingProperties.REPORTTYPE;

var customerAccountId = STATIC.customerAccountId;
var ACTION = bingProperties.ACTION;
var developerToken = STATIC.developerToken;
var password = STATIC.password;
var userName = STATIC.userName;
var startDate = new myDate('2013-01-01');
var endDate = new myDate('2013-01-01');

var ReportRequest = require('./report').ReportRequest;
// r();
var stop = 10;
for(var i = 0; i < stop; i++) {
    r(startDate.toString(), endDate.toString());
    startDate.addDate(1);
    endDate.addDate(1);
}

function r(s, e) {

    var startDate = new myDate(s);
    var endDate = new myDate(e);
    var report = new ReportRequest();
    // console.log(r,e);
    report.setCustomerAccountId(customerAccountId);
    report.setDeveloperToken(developerToken);
    report.setPassword(password);
    report.setUserName(userName);
    report.setAction(ACTION.SubmitGenerateReport);

    var adDyTextReport = report.createAdDynamicTextPerformanceReportRequest();
    var format = report.createReportFormat('Xml');
    var language = report.createReportLanguage('English');
    var d = new Date();
    var time = s + '_' + e //
    var reportName = report.createReportName('AdDynamicTextPerformanceReportRequest_' + time);
    var returnOnlyCompleteData = report.createReturnOnlyCompleteData(true);
    adDyTextReport.setFormat(format.target);
    adDyTextReport.setLanguage(language.target);
    adDyTextReport.setReportName(reportName.target);
    adDyTextReport.setReturnOnlyCompleteData(returnOnlyCompleteData.target);
    adDyTextReport.setAggregation('Daily');
    var columns = report.createAdDynamicTextPerformanceReportColumn();
    columns.addColumn('AccountName');
    columns.addColumn('AccountNumber');
    columns.addColumn('TimePeriod');
    // columns.addColumn('Status');
    // columns.addColumn('CampaignName');
    // columns.addColumn('CampaignId');
    // columns.addColumn('CurrencyCode');
    // columns.addColumn('AdDistribution');
    columns.addColumn('LanguageAndRegion'); //
    columns.addColumn('AdGroupName');
    columns.addColumn('AdGroupId');
    columns.addColumn('Keyword');
    // columns.addColumn('KeywordId');
    columns.addColumn('AdId');
    columns.addColumn('AdTitle'); //
    columns.addColumn('AdType'); //
    columns.addColumn('DestinationUrl');
    columns.addColumn('Param1');
    columns.addColumn('Param2');
    columns.addColumn('Param3');
    columns.addColumn('CurrencyCode');
    columns.addColumn('AdDistribution');
    // columns.addColumn('CurrentMaxCpc');
    // columns.addColumn('MatchType');
    /*
     var adDistribution = report.createAdDistribution('Search');
     columns.setAdDistribution(adDistribution.target);
    */
    columns.addColumn('Impressions');
    columns.addColumn('Clicks');
    columns.addColumn('Ctr');
    columns.addColumn('AverageCpc');
    columns.addColumn('Spend');
    columns.addColumn('AveragePosition');
    columns.addColumn('Conversions');
    columns.addColumn('ConversionRate');
    columns.addColumn('CostPerConversion');
    // columns.addColumn('LowQualityClicks');
    // columns.addColumn('LowQualityClicksPercent');
    // columns.addColumn('LowQualityImpressions');
    // columns.addColumn('LowQualityImpressionsPercent');
    // columns.addColumn('LowQualityConversions');
    // columns.addColumn('LowQualityConversionRate');
    columns.addColumn('AverageCpm');
    columns.addColumn('DeviceType');
    columns.addColumn('PricingModel');
    columns.addColumn('Language');
    // columns.addColumn('ImpressionLostToBudgetPercent');
    // columns.addColumn('ImpressionLostToRankPercent');
    // columns.addColumn('ImpressionLostToRelevancePercent');
    // columns.addColumn('ImpressionLostToOthersPercent');
    // columns.addColumn('ImpressionLostToBidPercent');
    // columns.addColumn('ImpressionLostToLandingPageRelevancePercent');
    // columns.addColumn('ImpressionLostToKeywordRelevancePercent');
    // columns.addColumn('QualityScore');
    // columns.addColumn('KeywordRelevance');
    // columns.addColumn('LandingPageRelevance');
    // columns.addColumn('LandingPageUserExperience');
    // columns.addColumn('HistoricQualityScore');
    // columns.addColumn('HistoricKeywordRelevance');
    // columns.addColumn('HistoricLandingPageRelevance');
    // columns.addColumn('HistoricLandingPageUserExperience');
    // columns.addColumn('ImpressionLostToBidPercent');
    // columns.addColumn('ImpressionLostToLandingPageRelevancePercent');
    // columns.addColumn('ImpressionLostToKeywordRelevancePercent');
    adDyTextReport.setColumns(columns.target);
    // var filter = report.createFilter();
    var accountIds = report.createAccountIds();
    accountIds.addAccountId(customerAccountId);
    var scope = report.createScope();
    scope.setAccountIds(accountIds.target);
    adDyTextReport.setScope(scope.target);
    var time = report.createTime();
    // time.setPredefinedTime('Yesterday');
    // console.log('-----------------------------------------------------------',mDate.toString());
    time.setCustomDateRangeEnd(endDate.toString()); //YYYY-MM-DD
    time.setCustomDateRangeStart(startDate.toString()); //YYYY-MM-DD
    adDyTextReport.setTime(time.target);



    report.on('reportError', function(error) {
        console.log('reportError \n', error.xml.toString(), '\n', error.header.toString(), '\n');
    });
    report.on('reportDataReady', function(reportURL) {
        var endTime = new Date();
        console.log('--------------------------- reportDataReady \n', reportURL);
        console.log('=====================time consuming-------------create report', (endTime.getTime() - startTime.getTime()) / 1000 + 's');
        startTime = endTime;
    });
    report.on('reportDataComplete', function(filePath) {
        var endTime = new Date();
        console.log('---------------------------reportDataComplete ', endTime);
        console.log('=====================time consuming-------------download report', (endTime.getTime() - startTime.getTime()) / 1000 + 's');
        console.log('reportDataComplete download file path is ', filePath);
    });
    var startTime = new Date()
    console.log('---------------------------reportStart ', startTime);
    var r = new bingAPI.Reporting();
    r.reportingService(ACTION.SubmitGenerateReport, report);
}